﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class AdManager1 : MonoBehaviour
{
    /// <summary>
    /// 是否是屏蔽区域—默认被屏蔽，白包
    /// </summary>
    public bool ShieldAddress = true;
    //获取的json数据
    private string GetjsonText;
    public bool isInit = false;
    //屏蔽区域 "厦门市",
    string[] PinBiAddress = new string[] {"石家庄", "太原市", "沈阳市", "长春市", "哈尔滨市", "南京市", "杭州市", "合肥市",
                            "福州市", "南昌市", "济南市", "郑州市", "长沙市", "武汉市", "海口市", "成都市", "贵阳市", "昆明市",
                            "西安市", "兰州市", "西宁市", "北京市", "天津市", "上海市", "重庆市", "大连市", "青岛市", "广东省" };
    public static AdManager1 _instance;
    //public static bool isyuanshengshow;

    public static AdManager1 instance
    {
        get
        {
            //初始化
            if (_instance == null)
            {
                GameObject gameObject = new GameObject("AdManager1");
                _instance = gameObject.AddComponent<AdManager1>();
                DontDestroyOnLoad(gameObject);
            }
            return _instance;
        }
    }
    
    /// <summary>
    /// 实例化黑白包
    /// </summary>
    public void InitData()
    {
        //ShieldAddress = ZTool.InShieldTime();
        StartCoroutine(GetUrlText());
        if (isInit) return;
        //InvokeRepeating("ShowYuanSheng", 30, 30);
    }

    IEnumerator GetUrlText()
    {
        UnityWebRequest request = UnityWebRequest.Get("https://yxapi.tomatojoy.cn/getIp");
        request.timeout = 2;//超时时间
        yield return request.SendWebRequest();
        try
        {
            //是否下载完成
            if (request.isDone)
            {
                if (request.isHttpError || request.isNetworkError)
                {
                    //    Debug.Log("下载失败"+request.error);
                    ShieldAddress = true;
                }
                else
                {
                    GetjsonText = request.downloadHandler.text;
                    Debug.Log("下载成功！！" + GetjsonText);
                    JsonData jsdata = JsonUtility.FromJson<JsonData>(GetjsonText);
                    Debug.Log("当前城市" + jsdata.data.city);
                    for (int i = 0; i < PinBiAddress.Length; i++)
                    {
                        if (jsdata.data.province == "广东省")
                        {
                            break;
                        }
                        if (jsdata.data.city == PinBiAddress[i])
                        {
                            // Debug.Log("当前处于屏蔽区域内");
                            ShieldAddress = true;
                            break;
                        }
                        else
                        {
                            ShieldAddress = false;
                        }
                    }
                }
            }
            Debug.Log("当前是否白包" + ShieldAddress);
        }
        catch (System.Exception)
        {
            Debug.Log("加载失败");
            throw;
        }
    }
    IEnumerator YuanSheng()
    {
        Debug.Log("执行");
        if (!ShieldAddress)
        {
            if(!AdManager1.instance.ShieldAddress) AdManager.single.ShowNative();
        }
        yield return 30;
    }
    void ShowYuanSheng()
    {
        Debug.Log("执行");
        if (!ShieldAddress)
        {
            isInit = true;
            if(!AdManager1.instance.ShieldAddress) AdManager.single.ShowNative();
            AdManager.single.ShowBanner();
        }
    }
}
[System.Serializable]
class data
{
    public string country;     //国家
    public string province;    //省份
    public string city;        //城市
    public string ip;          //ip
    public string server_time; //服务器时间
}
[System.Serializable]
class JsonData
{
    public string code;
    public data data;
}
